#!/usr/bin/env python
#coding=utf-8
import urllib2, re, urlparse, sys

def getchapter(url,name="unamed",mylog=None):
    "parser html source to generate chapterlist"
    cptre = re.compile(r'<div id=b><img src=/images/i.gif hspace=3><a  class=link01 href=(.*?) target=_blank>(.*?)</a></div>')
    chapterlist = []
    content = ''
    try:
        content = urllib2.urlopen(url).read()
        if content:
            for item in cptre.findall(content):
                title = item[1].decode('gbk').encode('utf-8')
                chapterlist.append([urlparse.urljoin(url,item[0]),title])
        return chapterlist
    except  Exception, e:
        if mylog:
            mylog.writeLog(str(e)+'in getchapter','error')
        return None
